package com.example.controller;

import com.csq.User;
import com.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 使用JavaBean传参时，请求方法类型为POST,不能为GET
 *
 * 改服务与provider2形成一个集群，相同服务得名称必须相同
 */
@RestController
public class UserController {

    private Logger log =  LoggerFactory.getLogger(UserController.class);

    @Value(value = "${server.port}")
    Integer port;

    @Resource
    private UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(@RequestBody User user){
        log.info("port:" + port);
        if("admin".equals(user.getUsername()) && "123".equals(user.getPassword()))
            return "success ," + user.getUsername()+ ",port" + port;
        else
            return "fail: " + user.getUsername()+ ",port" + port;

    }


    /**
     * 获取user对象
     * @return
     */
    @RequestMapping(value = "get/{username}", method = RequestMethod.GET)
    public User get(@PathVariable("username") String username){
        User user = userService.get(username);
        return  user;
    }
}
